-- UISlimeDesc
-- Create by huanghr
-- 冈布奥说明界面

UISlimeDesc = class("UISlimeDesc",function()
    return cc.Layer:create();
end
);

function UISlimeDesc.create(slimeId)
    return UISlimeDesc.new(slimeId);
end

-- 背景框切片数量
local SMALL_BG_COUNT = 4;

-- 构造函数
function UISlimeDesc:ctor(slimeId)
    -- 初始化
    self:setName("UISlimeDesc");
    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);

    local node = cc.CSLoader:createNode("layout/talent/SlimeDesc.csb");
    self:addChild(node);
    self.node = node;
    local CT = node:getChildByName("CT");

    self.CT = CT;

    -- self.petId = slimeId;

    -- 注册点击事件
    self:registerTouchEvent();

    -- 注册事件处理回调函数
    self:registerEventCallback()

    self:redraw();

    -- 适配
    self:resize(node);
end

-- 注册事件处理回调函数
function UISlimeDesc:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UISlimeDesc 界面析构清理");

            EventMgr.removeAll("UISlimeDesc");
        end
    end);
end

function UISlimeDesc:redraw()
    -- 设置冈布奥名称
    local title = findChildByName(self.CT, "name_label");
    local explain = findChildByName(self.CT, "desc_label");
    TextStyleM.setTextStyle(title, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    TextStyleM.setTextStyle(explain, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);

    title:setString(getLocStr("slime_info_desc"));
    explain:setString(getLocStr("talent_activate_help_desc1"));

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_SMALL, { title, explain, });
end

--适配
function UISlimeDesc:resize(node)
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册点击事件
function UISlimeDesc:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UISlimeDesc");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册任意地方点击返回事件
    local btnBgBack = findChildByName(self.node, "bg/bgImg");
    local function onBgBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UISlimeDesc");
        end
    end
    btnBgBack:addTouchEventListener(onBgBackClick);

    -- 防止点击事件被吞掉
    local bg = findChildByName(self.node, "CT/bg");
    for i = 1, SMALL_BG_COUNT do
        local subBg = findChildByName(bg, "bg" .. i);
        if subBg ~= nil then
            subBg:setSwallowTouches(false);
        end
    end
end
